package com.arc.level;

import arc.ArcServer;
import com.arc.message.SerializableVector;
import com.jme3.app.Application;
import com.jme3.app.state.AppStateManager;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ServerLevelState extends AbstractLevelState {  
    private ArcServer server;
    private Snapshot nextSnapshot;
    
    public ServerLevelState()
    {   
        super(Type.Server, snapshotSize); 
    }
    
    private void dbgOutputTree(Node node){
        System.out.print(node.getName() + " ");
        for(Spatial child : node.getChildren()){
            if(child instanceof Node)
                dbgOutputTree((Node)child);
        }
        
        System.out.println();
    }
    
    private void dbgOutputTree(NetworkedNode node){
        System.out.print(node.getName() + " ");
        for(NetworkedNode child : node.getChildren()){
            dbgOutputTree(child);
        }
        
        System.out.println();
    }
    
    @Override
    public void initialize(AppStateManager stateManager, Application app)
    {
        super.initialize(stateManager, app);
        server = (ArcServer)app;
        
        System.out.println("[Server state] Initializing level");        
        System.out.println("[Server] Level initialized");       
    }
    
    @Override
    public void update(float tpf)
    {
        
    }

    //This function sumbits a change to the 
    public synchronized boolean submitChange(Snapshot change) {
        return false;
    }
    
    //This function generates a snapshot that holds all of the changes since the last snapshot.
    /*public synchronized Snapshot generateSnapshot(int snapshotId)
    {
        Snapshot newSnap = new Snapshot();
        Snapshot[] changeArray = new Snapshot[nextSnapshotChanges.size()];
        nextSnapshotChanges.toArray(changeArray);
        nextSnapshotChanges.clear();
        
        newSnap.changeList = changeArray;
        newSnap.id   = snapshotId;
        
        if(snapshotHistory.get(0) == null)
        {                   
            snapshotHistory.set(0, newSnap);
        }else{ 
            pushSnapshot(newSnap);                        
        }
        
        return snapshotHistory.get(0);
    }   */
    
    //This function retrieves the latest snapshot and pushes it to snapshot memory.
    public synchronized Snapshot getSnapshot(){
        pushSnapshot(nextSnapshot);
        nextSnapshot = null;
        return getSnapshot(0);
    }
    
}
